package E3;

/**
 * WjsComplex
 * @author Hy
 * @date 2019/4/28
 */
public class WjsComplex{
    private double r;
    private double i;

    public WjsComplex(double r, double i) {
        this.r = r;
        this.i = i;
    }

    public static double getRealPart(double r) {
        return r;
    }

    public static double getImagePart(double i) {
        return i;
    }

    public WjsComplex complexAdd(WjsComplex a) {
        return new WjsComplex(r + a.r, i + a.i);
    }
    public WjsComplex complexSub(WjsComplex a) {
        return new WjsComplex(r - a.r, i - a.i);
    }
    public WjsComplex complexMulti(WjsComplex a) {
        return new WjsComplex(r * a.r - i * a.i, r * a.i + i * a.r);
    }
    public WjsComplex complexDiv(WjsComplex a) {
        return new WjsComplex((r * a.i + i * a.r)/(a.i * a.i + a.r * a.r), (i * a.i + r * a.r)/(a.i * a.i + a.r * a.r));
    }

    @Override
    public String toString() {
        String s = " ";
        if (i > 0) {
            s =  r + "+" + i + "i";
        }
        if (i == 0) {
            s =  r + "";
        }
        if (i < 0) {
            s = r + " " + i + "i";
        }
        return s;
    }
}